package cn.baiweigang.uu.service.fetch;

import cn.baiweigang.uu.db.dataobj.StockInfoDo;
import cn.baiweigang.uu.utils.StringUtil;
import com.alibaba.fastjson.JSONArray;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class SohuService extends FetchBaseService {

    public void StockHistoryDayForCN(String start, String end) {
//        String start = DateUtil.formatDate(new Date(), "yyyyMMdd");
////        String start = "20220901";
//        String end = DateUtil.formatDate(new Date(), "yyyyMMdd");

        start = start.replaceAll("-", "");
        end = end.replaceAll("-", "");
        List<StockInfoDo> stockInfoDoList = baseInfoRepository.queryCN();
        List<StockInfoDo> needSave = new ArrayList<>();
        //ST的票 先不获取了
        for (int i = 0; i < stockInfoDoList.size(); i++) {
            StockInfoDo stockInfoDo = stockInfoDoList.get(i);
            if (isST(stockInfoDo)) {
                continue;
            }

            try {
                List<StockInfoDo> tmp = pullStockHistoryDay(stockInfoDo, start, end);
                //暂停200ms  防止请求太频繁
                Thread.sleep(200);

                if (tmp != null) {
                    needSave.addAll(tmp);
                }
            } catch (Throwable e) {
                log.error("获取异常 dm=" + stockInfoDo.getDm());
                log.error(e.getMessage());
            }
        }
        //写DB
        if (needSave.size() > 0) {
            historyDayInfoRepository.insertOrUpdateBatch(needSave);
        }

    }

    /**
     * 获取日线历史数据
     */
    public List<StockInfoDo> pullStockHistoryDay(StockInfoDo stockInfoDo, String start, String end) {
        String url = "http://q.stock.sohu.com/hisHq";
        Map<String, String> param = new HashMap<>();
        param.put("start", start);
        param.put("end", end);
        param.put("stat", "0");
        param.put("order", "D");
        param.put("period", "d");
        param.put("code", "cn_" + stockInfoDo.getDm());

        log.info("开始请求sohu 获取历史数据 " + stockInfoDo.getDm() + " " + stockInfoDo.getMc());
        String resp = httpService.doGet(url, null, param);
        if (StringUtil.isNotBlank(resp) && resp.length() > 10) {
            JSONArray jsonArray = JSONArray.parseArray(resp);
            if (jsonArray.size() > 0) {
                JSONArray hq = jsonArray.getJSONObject(0).getJSONArray("hq");
                //存入DB
                return getStockInfoList(stockInfoDo, hq);
            }
        } else {
            log.error("解析返回的数据异常 resp=" + resp);
        }
        return null;
    }

    private List<StockInfoDo> getStockInfoList(StockInfoDo stockInfoDo, JSONArray hq) {
        List<StockInfoDo> stockInfoDoList = new ArrayList<>();
        if (hq != null && hq.size() > 0) {
            for (int i = 0; i < hq.size(); i++) {
                JSONArray day = hq.getJSONArray(i);
                StockInfoDo tmp = new StockInfoDo();
                tmp.setMc(stockInfoDo.getMc());
                tmp.setDm(stockInfoDo.getDm());

                //解析搜返回数据   10个指标
            /*
             [
                "2022-11-04", 日期
                "58.59", 开盘
                "60.60", 收盘
                "2.03", 涨跌额
                "3.47%", 涨跌 %
                "58.50", 最低
                "62.28", 最高
                "213539", 成交量 手数
                "129592.96", 成交额
                "1.95%" 换手率
            ]
             */
                tmp.setDs(day.getString(0));
                tmp.setO(day.getDouble(1));
                tmp.setC(day.getDouble(2));
                tmp.setZde(day.getDouble(3));
                tmp.setZd(removePercent(day.getString(4)));
                tmp.setL(day.getDouble(5));
                tmp.setH(day.getDouble(6));
                tmp.setV(day.getLong(7) * 100);
                tmp.setE(day.getDouble(8));
                tmp.setHs(removePercent(day.getString(9)));
                tmp.setExt("{}");
                stockInfoDoList.add(tmp);
            }
        }
        return stockInfoDoList;
    }

    private Double removePercent(String value) {
        try {
            return Double.valueOf(value.substring(0, value.length() - 1));
        } catch (Throwable e) {
            log.error("value=" + value + " 转换成double报错 ");
        }
        return 0.00;
    }
}
